<?php

namespace plugin\coupon\app\model;

use think\Model;

/**
 * t_coupon 优惠券
 * @property integer $id 优惠券id(主键)
 * @property string $name 优惠券名
 * @property integer $type 类型 1:满减券 2:折扣券
 * @property string $discount 折扣额
 * @property string $max_discount_amount 最大优惠金额
 * @property string $amount 优惠券面额
 * @property integer $is_limit_num 是否限制发放数量 1:是 2:否
 * @property integer $total_num 发放总数量
 * @property integer $received_num 累计领取数量
 * @property integer $used_num 使用数量
 * @property integer $max_receive_num 每人最大领取数量
 * @property integer $is_threshold 是否有门槛 1:是 2:否
 * @property string $threshold_amount 门槛金额
 * @property integer $status 状态 1:进行中 2:已作废 3:已过期 4:已领完
 * @property integer $validity_type 有效期类型 1:固定日期 2:领取之后
 * @property string $start_time 开始日期
 * @property string $end_time 结束时间
 * @property integer $receive_useful_day 领取之后的有效期天数
 * @property integer $join_goods 商品参与方式 1:全部 2:指定商品
 * @property string $create_time 创建时间
 * @property string $update_time 更新时间
 * @property string $goods_category_ids 指定商品分类可用 不设置就不限制
 * @property string $goods_brand_ids 指定商品品牌可用 不设置就不限制
 * @property string $goods_tag_ids 指定商品标签可用 不设置就不限制
 * @property string $user_group_ids 指定用户分组可用 不设置就不限制
 * @property string $user_level_ids 指定用户等级可用 不设置就不限制
 * @property string $user_label_ids 指定用户标签可用 不设置就不限制
 */
class Coupon extends Model
{
    /**
     * The connection name for the model.
     *
     * @var string|null
     */
    protected $connection = 'mysql';

    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 't_coupon';

    /**
     * The primary key associated with the table.
     *
     * @var string
     */
    protected $pk = 'id';

    const STATUS_NORMAL = 1;
    const STATUS_ENABLE = 2;
    const STATUS_VOIDED = 3;
    const STATUS_EXPIRED = 4;
    const STATUS_NO_STOCK = 5;
    const STATUS_TEXT_MAP = [
        self::STATUS_NORMAL => "待启用",
        self::STATUS_ENABLE => "进行中",
        self::STATUS_VOIDED => "已作废",
        self::STATUS_EXPIRED => "已过期",
        self::STATUS_NO_STOCK => "已领完",
    ];

    const VALIDITY_TYPE_FIXED = 1;
    const VALIDITY_TYPE_AFTER_RECEIVING = 2;


    //1:固定日期 2:领取之后
    const VALIDITY_TYPE_TEXT_MAP = [
        self::VALIDITY_TYPE_FIXED => "固定日期",
        self::VALIDITY_TYPE_AFTER_RECEIVING => "领取后N天",
    ];


    const JOIN_GOODS_ALL = 1; //全部商品
    const JOIN_GOODS_DESIGNATED = 2; //指定商品


    protected $append = [
        'status_text',
        'validity_type_text',
        'join_goods_text',
    ];

    public function getStatusTextAttr($value, $data)
    {
        return self::STATUS_TEXT_MAP[$data['status']] ?? "";
    }

    public function getValidityTypeTextAttr($value, $data)
    {
        return self::VALIDITY_TYPE_TEXT_MAP[$data['validity_type']] ?? "";
    }

    public function getJoinGoodsTextAttr($value, $data)
    {
        return $data['join_goods'] == 1 ? "全部商品" : "指定商品";
    }


    public function getGoodsCategoryIdsAttr($value)
    {
        return array_map("intval", $value ? explode(",", $value) : []);
    }

    public function getGoodsBrandIdsAttr($value)
    {
        return array_map("intval", $value ? explode(",", $value) : []);
    }

    public function getGoodsTagIdsAttr($value)
    {
        return array_map("intval", $value ? explode(",", $value) : []);
    }

    public function getUserGroupIdsAttr($value)
    {
        return array_map("intval", $value ? explode(",", $value) : []);
    }

    public function getUserLevelIdsAttr($value)
    {
        return array_map("intval", $value ? explode(",", $value) : []);
    }

    public function getUserLabelIdsAttr($value)
    {
        return array_map("intval", $value ? explode(",", $value) : []);
    }

    public function setGoodsCategoryIdsAttr($value)
    {
        return implode(',', $value);
    }

    public function setGoodsBrandIdsAttr($value)
    {
        return implode(',', $value);
    }

    public function setGoodsTagIdsAttr($value)
    {
        return implode(',', $value);
    }

    public function setUserGroupIdsAttr($value)
    {
        return implode(',', $value);
    }

    public function setUserLevelIdsAttr($value)
    {
        return implode(',', $value);
    }

    public function setUserLabelIdsAttr($value)
    {
        return implode(',', $value);
    }


}
